package krow.dev.gunpobus;

import krow.dev.gunpobus.control.FavoriteCountTask;
import krow.dev.gunpobus.control.http.HtmlResponseListener;
import krow.dev.gunpobus.control.http.RequestManager;
import krow.dev.gunpobus.control.parser.RouteParser;
import krow.dev.gunpobus.control.parser.StationParser;
import krow.dev.gunpobus.util.AnimationUtil;
import krow.dev.gunpobus.util.DebugHelper;
import krow.dev.gunpobus.view.dialog.ConfirmDialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.android.volley.Request;
import com.android.volley.VolleyError;

public class SplashActivity extends Activity {

	private GlobalStore mGlobalStore;
	
	private FavoriteCountTask mFavoriteCountTask;
	
	private Request<?> mRequest;
	
	private Dialog mDialog;
	
	private Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		
		mGlobalStore = GlobalStore.getInstance();
		
		View innerView = findViewById(R.id.view_progress_inner);
		innerView.startAnimation(AnimationUtil.createRotateAnimation(this, 720, 0));
		View outerView = findViewById(R.id.view_progress_outer);
		outerView.startAnimation(AnimationUtil.createRotateAnimation(this, 0, 1080));
	}

	@Override
	protected void onResume() {
		super.onResume();
		checkFavorite();
	}
	
	@Override
	protected void onPause() {
		if(mFavoriteCountTask != null){
			mFavoriteCountTask.cancel(true);
		}
		if(mRequest != null){
			mRequest.cancel();
		}
		if(mDialog != null){
			mDialog.dismiss();
		}
		mHandler.removeCallbacks(mRetryRunnable);
		mHandler.removeCallbacks(mStartRunnable);
		super.onPause();
	}
	
	private void checkFavorite(){
		mFavoriteCountTask = new FavoriteCountTask();
		mFavoriteCountTask.setCompletionListener(mCheckCompletionListener);
		mFavoriteCountTask.execute();
	}
	
	private void requestRoute() {
		mRequest = RequestManager.requestRoute(mRouteResponseListener);
	}

	private void requestStation() {
		mRequest = RequestManager.requestStation(mStationResponseListener);
	}
	
	private void startMainActivity(){
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
	}
	
	private void handleNetworkError(){
		if(mDialog != null){
			mDialog.dismiss();
		}
		
		final ConfirmDialog confirmDialog = new ConfirmDialog(this);
		confirmDialog.setContent(R.string.text_network_error);
		confirmDialog.setPositiveButton(
				R.string.button_retry, 
				new View.OnClickListener() {
					
					@Override
					public void onClick(View view) {
						confirmDialog.dismiss();
						requestRoute();
					}
				});
		confirmDialog.setNegativeButton(
				R.string.button_cancel,
				new View.OnClickListener() {
					
					@Override
					public void onClick(View view) {
						confirmDialog.dismiss();
						finish();
					}
				});
		mDialog = confirmDialog;
		mDialog.show();
	}
	
	private FavoriteCountTask.OnCompletionListener mCheckCompletionListener = new FavoriteCountTask.OnCompletionListener() {
		
		@Override
		public void onCompletion(int count) {
			DebugHelper.i("SplashActivity - favorite item count: " + count);
			if(count > 0){
				mGlobalStore.setStartPageIndex(0);
			}else{
				mGlobalStore.setStartPageIndex(1);
			}
			
			if(mGlobalStore.isAvailable()){
				mHandler.postDelayed(mStartRunnable, 1000);
			}else{
				requestRoute();
			}
		}
	};
	
	private HtmlResponseListener<RouteParser> mRouteResponseListener = new HtmlResponseListener<RouteParser>() {

		@Override
		public void onResponse(RouteParser response) {
			mGlobalStore.setRouteItemMap(response.getRouteItemMap());
			mGlobalStore.setRouteItemList(response.getRouteItemList());
			requestStation();
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			mHandler.postDelayed(mRetryRunnable, 1000);
		}
	};

	private HtmlResponseListener<StationParser> mStationResponseListener = new HtmlResponseListener<StationParser>() {

		@Override
		public void onResponse(StationParser response) {
			mGlobalStore.setStationItemMap(response.getStationItemMap());
			mGlobalStore.setStationItemList(response.getRouteItemList());
			startMainActivity();
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			mHandler.postDelayed(mRetryRunnable, 1000);
		}
	};
	
	private Runnable mRetryRunnable = new Runnable() {
		
		@Override
		public void run() {
			handleNetworkError();
		}
	};
	
	private Runnable mStartRunnable = new Runnable() {
		
		@Override
		public void run() {
			startMainActivity();
		}
	};
}
